package com.baiytfp.hf.HFDataExchange.common.utils;

import com.baiytfp.hf.baiytfp.framework.model.state.YesOrNo;
import com.baiytfp.hf.baiytfp.hfutils.HFConfig;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_account;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_info;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateModelException;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class UserUtil {

	// 取得用户名
	public static String getUserName(String userCode) throws Exception {

		User_info userInfoVO = getUserInfo(userCode);
		return userInfoVO.getUser_name();
	}

	// 取得用户角色名称
//	public static String getRoleName(String userCode) throws Exception {
//		UserService us = SpringUtil.getBean("userService");
//
//		User_account userVO = us.getUser(userCode);
//		String roleName = us.getRoleNameList(userVO.getUser_role());
//		return roleName;
//	}

	// 取得用户权限
//	public static String getUserRight(HttpServletRequest request)
//			throws Exception {
//		if (!"".equals(RequestUtil.getUserCode(request))) {
//			/*
//			 * OuterNetUserService onus = new OuterNetUserService();
//			 * OuterNetUserVO outerNetUserVO =
//			 * onus.getUserInfoByName(RequestUtil.getUserCode(request)); return
//			 * outerNetUserVO.getCheck_state();
//			 */
//		}
//		return "0";
//	}

	/* 获取是否超级管理员 */
	public static boolean isAdmin(String userCode) throws Exception {

		User_account userAccountVO = getUsersAccount(userCode);
		if (userAccountVO.getIs_admin() == 1) {
			return true;
		}
		return false;
	}

	/* 判断权限 */
//	public static boolean checkRight(HttpServletRequest request,
//									 HttpServletResponse response) throws Exception {
//		String userCode = StringUtil.getString(request.getRemoteUser(), "");
//		if (isAdmin(userCode)) {
//			return true;
//		} else {
//			ResponseUtil.responseTextMsg(response, "您没有权限操作此页面, 谢谢合作！");
//			return false;
//		}
//	}


	public static User_account getUsersAccount(String userCode) throws Exception {

		User_account userAccountVO = HFDBHelper.read(TableName.USER_ACCOUNT, " where user_code=? ", User_account.class, userCode);

		return userAccountVO == null ? new User_account() : userAccountVO;
	}

	// 取得用户角色名称
	public static User_info getUserInfo(String userCode) throws Exception {

		User_info userInfoVO = HFDBHelper.read(TableName.USER_INFO, " where user_code=? ", User_info.class, userCode);

		return userInfoVO == null ? new User_info() : userInfoVO;
	}
}
